package branchws;

import accountws.SessionExpiredException;
import bookdbws.Book;
import bookdbws.BookdbWS;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;

/**
 *
 * @author Atuan
 */
@WebService(serviceName = "BranchWS")
public class BranchWS {
    
    private IBranchDB dbh;

    public BranchWS() {
        dbh = new DBHandler();
    }
    
    @WebMethod(operationName = "removeorder")
    public Boolean removeOrder(int id, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.removeOrder(id);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "addorder")
    public Boolean addOrder(Order order, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.addOrder(order);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getorder")
    public Order getOrder(int id, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getOrder(id);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getallorders")
    public List<Order> getAllOrders(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getAllOrders();
        } else {
            throw new SessionExpiredException();
        }
    }    
    
    @WebMethod(operationName = "getfirstavailableOrderid")
    public int getFirstAvailableOrderId(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getFirstAvailableOrderIndex();
        } else {
            throw new SessionExpiredException();
        }
    }

    @WebMethod(operationName = "getfirstavailableClientid")
    public int getFirstAvailableClientId(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getFirstAvailableClientsId();
        } else {
            throw new SessionExpiredException();
        }
    }
        
    @WebMethod(operationName = "getallclients")
    public List<Client> getAllClients(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getAllClients();
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "processorders")
    public Boolean processOrders(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.processOrders();
        } else {
            throw new SessionExpiredException();
        }
    }

    @WebMethod(operationName = "test")
    public void test(){
        try {
            String sid = "439077901928683112828694admin";
            Order o = new Order();
            Client c = new Client(getFirstAvailableClientId(sid),"seba","sebowy");
            o.setClient(c);
            o.setId(getFirstAvailableOrderId(sid));
            BookdbWS bdws = new BookdbWS();
            Book b = bdws.getBook("978-0-57507-921-2", sid);
            OrderItem oi = new OrderItem(b,1);
            o.addItem(oi);
            b = bdws.getBook("0-345-40447-5", sid);
            oi = new OrderItem(b,1);
            o.addItem(oi);
            addOrder(o,sid);
            //
            o = new Order();
            o.setId(getFirstAvailableOrderId(sid));
            c = new Client(getFirstAvailableClientId(sid),"test","tostowy");
            o.setClient(c);
            b = bdws.getBook("978-83-89640-41-3", sid);
            oi = new OrderItem(b,1);
            o.addItem(oi);
            addOrder(o,sid);
            //
            o = new Order();
            o.setId(getFirstAvailableOrderId(sid));
            c = new Client(getFirstAvailableClientId(sid),"the","killing");
            o.setClient(c);
            b = bdws.getBook("0-553-10354-7", sid);
            oi = new OrderItem(b,4);
            o.addItem(oi);
            addOrder(o,sid);
            
            removeOrder(2,sid);
        } catch(SessionExpiredException e){
            System.err.println("error: "+e.getMessage());
        }
    }
}
